﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using FriendlyFireLibrary;

namespace IndividualServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        FriendlyFireLibrary.NetworkAsyncCodeServer server;

        private void txtServerMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && txtServerMessage.Text != "")
            {
                for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
                    server.SendMessage(txtServerMessage.Text, server.networkClients[listBox1.SelectedIndices[i]].ClientSocket);
                txtServerOutput.Text += "Server : " + txtServerMessage.Text + Environment.NewLine;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < listBox1.SelectedIndices.Count; i++)
                server.SendMessage(txtServerMessage.Text, server.networkClients[listBox1.SelectedIndices[i]].ClientSocket);
            txtServerOutput.Text += "Server : " + txtServerMessage.Text + Environment.NewLine;
            txtServerMessage.Text = "";
        }

        private void MyFunc(byte[] data, int dataType)
        {
            if (dataType == (int)Enumerations.DataTypes.DataSet)
            {
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            server = new FriendlyFireLibrary.NetworkAsyncCodeServer(new RSAParameters(), new RSAParameters());
            server.dataReceivedEvent += new NetworkCodeCommon.DataReceived(server_dataReceivedEvent);
            //server.clientConnectedEvent += new NetworkCodeCommon.OnClientConnected(server_clientConnectedEvent);
            server.clientConnectedEvent += new NetworkCodeCommon.OnClientConnected(server_clientConnectedEvent);
            foreach (Control c in this.Controls)
            {
                c.ContextMenuStrip = contextMenuStrip1;
            }
        }

        void server_clientConnectedEvent(NetworkClient client)
        {
            //throw new NotImplementedException();
            AddClient(client.ClientName);
        }

        void server_clientConnectedEvent(string ClientName, System.Net.Sockets.Socket client)
        {
            AddClient(ClientName);
        }
        private delegate void OnAddClient(string item);
        public void AddClient(string item)
        {
            ListBox control = this.listBox1;
            if (control.InvokeRequired)
            {
                control.Invoke(new OnAddClient(AddClient), item);
            }
            else
            {
                control.Items.Add(item);
            }
        }

        void server_dataReceivedEvent(byte[] data, int dataType)
        {
            
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            for(int i = 0; i < listBox1.Items.Count; i++)
                listBox1.SelectedIndex = i;
        }

        private void txtServerOutput_TextChanged(object sender, EventArgs e)
        {
            txtServerOutput.SelectionStart = txtServerOutput.Text.Length;
            txtServerOutput.ScrollToCaret();
        }

        private void txtServerMessage_KeyPress(object sender, KeyPressEventArgs e)
        {
                    
        }

        private void txtServerMessage_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && txtServerMessage.Text != "")
            {
                txtServerMessage.Text = "";
            }
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show(new Point(MousePosition.X, MousePosition.Y));
            }
        }
        bool pause = false;
        private void pauseTransfersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pause = !pause;
            server.Pause(pause);
        }

        private void maxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(0);
            server.SetMaxRecv(0);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(10);
            server.SetMaxRecv(10);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(50);
            server.SetMaxRecv(50);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(100);
            server.SetMaxRecv(100);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(500);
            server.SetMaxRecv(500);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            server.SetMaxSend(1000);
            server.SetMaxRecv(1000);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] searchTerms = Encoding.ASCII.GetBytes("Hello");
            server.SendData(searchTerms, (int)Enumerations.DataTypes.String, server.networkClients[0].ClientSocket);
        }
    }
}
